package com.zlx.sharelive.fragment;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.zlx.module_base.base_api.res_data.MsgListBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_fg.BaseFg;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_base.base_util.StringUt;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.Constants;
import com.zlx.sharelive.R;
import com.zlx.sharelive.activity.CommentAc;
import com.zlx.sharelive.activity.LikeAc;
import com.zlx.sharelive.activity.MessageListAc;
import com.zlx.sharelive.activity.MyFansAc;
import com.zlx.sharelive.activity.NearbyAc;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.model.Conversation;

/**
 * 消息模块
 */
public class MessageFg extends BaseFg {

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_content)
    TextView tv_content;

    private int page = 1;

    private List<MsgListBean.InfoDTO.RecordsDTO> mList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fg_message;
    }

    @Override
    protected void initViews() {
        super.initViews();

        getMsgList(page);

        initFragment();
    }

    private void initFragment() {
        ConversationListFragment conversationListFragment = new ConversationListFragment();
        // 此处设置 Uri. 通过 appendQueryParameter 去设置所要支持的会话类型. 例如
        // .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(),"false")
        // 表示支持单聊会话, false 表示不聚合显示, true 则为聚合显示
        Uri uri = Uri.parse("rong://" +
                getActivity().getApplicationInfo().packageName).buildUpon()
                .appendPath("conversationlist")
                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话是否聚合显示
                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")//群组
                .appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")//公共服务号
                .appendQueryParameter(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName(), "false")//订阅号
                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "true")//系统
                .build();

        conversationListFragment.setUri(uri);
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.container, conversationListFragment);
        transaction.commit();
    }

    private void getMsgList(int page) {
        ApiUtil.getUserApi().getMsgList(SPUtil.getToken(getContext()),
                String.valueOf(page)).observe(getViewLifecycleOwner(), new BaseObserver<>(new BaseObserverCallBack<ApiResponse<MsgListBean>>() {
            @Override
            public void onSuccess(ApiResponse<MsgListBean> data) {
                mList.clear();
                if (data.getData().getCode() == 0) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                if (null != data.getData().getInfo().get(0).getRecords()) {
                                    if (data.getData().getInfo().get(0).getRecords().size() > 0) {
                                        mList.addAll(data.getData().getInfo().get(0).getRecords());
                                        setMessage();
                                    }
                                }
                            }
                        }
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }

    private void setMessage() {
        if (mList.size() > 0) {
            if (mList.get(0).getType().equals("1")) {
                tv_title.setText("好友申请");
            } else if (mList.get(0).getType().equals("2")) {
                tv_title.setText("系统消息");
            }
            tv_content.setText(mList.get(0).getMsgTitle());
        }
    }

    @OnClick({R.id.ll_message, R.id.ll_fan, R.id.ll_nearby, R.id.ll_praise, R.id.ll_comment})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.ll_message:
                startActivity(new Intent(getContext(), MessageListAc.class));
                break;
            case R.id.ll_fan:
                //粉丝
                startActivity(new Intent(getContext(), MyFansAc.class));
                break;
            case R.id.ll_nearby:
                //附近的人
                if (StringUt.isEmpty(String.valueOf(SPUtil.get(getContext(), Constants.SP_VIP, "")))) {
                    //未开通
                    Toast.makeText(getContext(), "该功能仅对会员开放", Toast.LENGTH_SHORT).show();
                    return;
                } else if (String.valueOf(SPUtil.get(getContext(), Constants.SP_VIP, "")).equals("1")) {
                    startActivity(new Intent(getContext(), NearbyAc.class));
                } else {
                    Toast.makeText(getContext(), "该功能仅对会员开放", Toast.LENGTH_SHORT).show();
                    return;
                }
                break;
            case R.id.ll_praise://点赞列表
                startActivity(new Intent(getContext(), LikeAc.class));
                break;
            case R.id.ll_comment://评论列表
                startActivity(new Intent(getContext(), CommentAc.class));
                break;
        }
    }
}
